//
//  XYRockerPointView.m
//  FECCView
//
//  Created by 杨旭东 on 2017/9/11.
//  Copyright © 2017年 JackYang. All rights reserved.
//

#import "XYRockerPointView.h"

@interface XYRockerPointView ()

@property (nonatomic, assign) CGFloat _radius;

@property (nonatomic, strong) UIImage *normal;

@property (nonatomic, strong) UIImage *highlight;

@property (nonatomic, copy) void (^BreakLimitBlock)();

@property (nonatomic, copy) void (^ResetBlock)(BOOL);

@property (nonatomic, copy) void (^DirectionBlock)();

@property (nonatomic, assign) XYRockerTrackType type;

@property (nonatomic, assign) BOOL currentVertical;

@end

@implementation XYRockerPointView

+ (XYRockerPointView *(^)(CGPoint))addXYRockerPoint{
    return ^(CGPoint origin){
        XYRockerPointView *rocker = [[XYRockerPointView alloc] initWithFrame:CGRectMake(origin.x, origin.y, 63, 63)];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:rocker action:@selector(panAction:)];
        [rocker addGestureRecognizer:pan];
        rocker.userInteractionEnabled = YES;
        rocker.type = XYRockerTrackType_Free;
        return rocker;
    };
}

- (XYRockerPointView *(^)(CGFloat))xyradius{
    return ^(CGFloat radius){
        __radius = radius;
        return self;
    };
}

- (XYRockerPointView *(^)(UIImage *, XYRockerPoint))background{
    return ^(UIImage *image, XYRockerPoint type){
        switch (type) {
            case XYRockerPoint_Normal:{
                _normal = image;
                self.image = _normal;
                break;
            }
                
            default:
                _highlight = image;
                break;
        }
        return self;
    };
}

- (void)breakLimit:(void (^)())breakLimit{
    self.BreakLimitBlock = breakLimit;
}

- (void)reset:(void (^)(BOOL))reset{
    self.ResetBlock = reset;
}

- (void)direction:(void (^)(XYRockerDirection))direction{
    self.DirectionBlock = direction;
}

- (XYRockerPointView *(^)(XYRockerTrackType))trackType{
    return ^(XYRockerTrackType type){
        _type = type;
        return self;
    };
}

- (void)panAction:(UIPanGestureRecognizer *)pan{
    CGPoint pointInproc;
    static CGRect frame;
    if (pan.state == UIGestureRecognizerStateBegan) {
        frame = self.frame;
    }
    
    pointInproc = [pan translationInView:self];
    
    if (sqrtf((pointInproc.x * pointInproc.x) + (pointInproc.y * pointInproc.y)) <= 43) {
        self.frame = CGRectMake(frame.origin.x + (_type==XYRockerTrackType_Vertical?0:pointInproc.x), frame.origin.y + (_type>XYRockerTrackType_Horizontal?pointInproc.y:0), self.bounds.size.width, self.bounds.size.height);
        [self decideDirection:pointInproc];
    }else{
        CGFloat x, y;
        if (pointInproc.x == 0 && pointInproc.y == 0) {
            x = y = 0;
        }else if (pointInproc.x == 0) {
            x = 0;
            y = 43 * (pointInproc.y > 0 ? 1 : -1);
        }else{
            CGFloat value = pointInproc.y / pointInproc.x;
            x = sqrtf(43 * 43 / (1 + value * value)) * (pointInproc.x > 0 ? 1 : -1);
            y = x * value;
            if (pointInproc.y * y < 0) {
                y = y * -1;
            }
        }
        self.frame = CGRectMake(frame.origin.x + (_type==XYRockerTrackType_Vertical?0:x), frame.origin.y + (_type>XYRockerTrackType_Horizontal?y:0), self.bounds.size.width, self.bounds.size.height);
        [self decideDirection:CGPointMake(x, y)];
    }
    
    if (pan.state == UIGestureRecognizerStateEnded) {
        frame = CGRectZero;
        self.ResetBlock(_currentVertical);
        self.image = _normal;
    }
}

- (void)setCurrentVertical:(BOOL)currentVertical{
    if (_currentVertical != currentVertical) {
        if (_currentVertical) {
            self.DirectionBlock(XYRockerDirection_Reset_Vertical);
        }else{
            self.DirectionBlock(XYRockerDirection_Reset_Horizontal);
        }
        _currentVertical = currentVertical;
    }
}

- (void)decideDirection:(CGPoint)point{
    if (_type==XYRockerTrackType_Free) {
        if (point.x > 0 && point.y > 0) {
            if ((point.x/point.y) > 1) {
                self.currentVertical = NO;
                self.DirectionBlock(XYRockerDirection_Right);
            }else{
                self.currentVertical = YES;
                self.DirectionBlock(XYRockerDirection_Down);
            }
        }
        
        if (point.x > 0 && point.y < 0) {
            if ((point.x/point.y) > -1) {
                self.currentVertical = YES;
                self.DirectionBlock(XYRockerDirection_Up);
            }else{
                self.currentVertical = NO;
                self.DirectionBlock(XYRockerDirection_Right);
            }
        }
        
        if (point.x < 0 && point.y > 0) {
            if ((point.x/point.y) > -1) {
                self.currentVertical = YES;
                self.DirectionBlock(XYRockerDirection_Down);
            }else{
                self.currentVertical = NO;
                self.DirectionBlock(XYRockerDirection_Left);
            }
        }
        
        if (point.x < 0 && point.y < 0) {
            if ((point.x/point.y) > 1) {
                self.currentVertical = NO;
                self.DirectionBlock(XYRockerDirection_Left);
            }else{
                self.currentVertical = YES;
                self.DirectionBlock(XYRockerDirection_Up);
            }
        }
    }else if (_type==XYRockerTrackType_Vertical){
        if (point.y > 0) {
            self.DirectionBlock(XYRockerDirection_Down);
        }else{
            self.DirectionBlock(XYRockerDirection_Up);
        }
        self.currentVertical = YES;
    }else{
        self.currentVertical = NO;
        self.DirectionBlock(point.x > 0 ? XYRockerDirection_Right : XYRockerDirection_Left);
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.image = _highlight;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.image = _normal;
    self.ResetBlock(_currentVertical);
}

@end
